package com.sk.weichat.ui.circle;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.luck.picture.lib.tools.ScreenUtils;
import com.sk.weichat.R;
import com.sk.weichat.adapter.TabFragmentPagerAdapter;
import com.sk.weichat.ui.base.LazyFragment;
import com.sk.weichat.view.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lzs
 * @date 2019/10/22
 * Email:343067508@qq.com
 * 动态列表
 **/
public class DynamicListFragment extends LazyFragment {
    List<Fragment> list = new ArrayList<>();
    ViewPager viewPager;
    TabFragmentPagerAdapter adapter;
    ImageView ivAddDynamic; //发布动态

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_dynamics_list;
    }


    @Override
    protected void loadData() {

    }

    @Override
    protected void initView(View view) {
        viewPager = view.findViewById(R.id.viewpager);
        ivAddDynamic = view.findViewById(R.id.iv_add_dynamic);
        ivAddDynamic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //flag 动态标记（ 1=求职消息、2=招聘消息、3=普通消息、4=广场动态，5=城市动态）
                int type = 0;
                if (viewPager.getCurrentItem() == 0) {
                    type = 3;
                } else {
                    type = 4;
                }
                //跳转发动态页面
                startActivity(new Intent(getActivity(), PublishDynamicActivity.class).putExtra("flag", type));
            }
        });
        MagicIndicator indicator = view.findViewById(R.id.magic_indicator);
        ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) indicator.getLayoutParams();
        lp.height += ScreenUtils.getStatusBarHeight(mContext);
        indicator.setLayoutParams(lp);
        indicator.setPadding(0, ScreenUtils.getStatusBarHeight(mContext), 0, 0);
        adapter = new TabFragmentPagerAdapter(getChildFragmentManager(), list);
        viewPager.setAdapter(adapter);
        initMagicIndicator(view);
        list.add(new FollowerDynamicsFragment());
        list.add(new DynamicSquareFragment());
        adapter.notifyDataSetChanged();
    }

    private static final List<String> indicatorTitles = new ArrayList<>();

    static {
        indicatorTitles.add("关注");
        indicatorTitles.add("广场");
    }

    private void initMagicIndicator(View root) {
        MagicIndicator magicIndicator = root.findViewById(R.id.magic_indicator);
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdjustMode(false);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return indicatorTitles == null ? 0 : indicatorTitles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(indicatorTitles.get(index));
                simplePagerTitleView.setTextSize(25);
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(getActivity(), R.color.text_color_main));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(getActivity(), R.color.text_color_main));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setLineHeight(UIUtil.dip2px(context, 8));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setColors(Color.parseColor("#40c4ff"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


}
